/***********************************************************************
 * Module:  GMainMenu.java
 * Author:  Voja
 * Purpose: Defines the Class GMainMenu
 ***********************************************************************/

package genedit.gui;

import genedit.controller.interfaceActions.GCloseA;
import genedit.controller.interfaceActions.GCloseAllA;
import genedit.controller.interfaceActions.GCopyA;
import genedit.controller.interfaceActions.GCutA;
import genedit.controller.interfaceActions.GDeleteA;
import genedit.controller.interfaceActions.GExitA;
import genedit.controller.interfaceActions.GGenerateCodeA;
import genedit.controller.interfaceActions.GNewDocumentA;
import genedit.controller.interfaceActions.GNewPackageA;
import genedit.controller.interfaceActions.GNewProjectA;
import genedit.controller.interfaceActions.GNewSubDocumentA;
import genedit.controller.interfaceActions.GOpenA;
import genedit.controller.interfaceActions.GPasteA;
import genedit.controller.interfaceActions.GPrintA;
import genedit.controller.interfaceActions.GRedoA;
import genedit.controller.interfaceActions.GSaveA;
import genedit.controller.interfaceActions.GSaveAllA;
import genedit.controller.interfaceActions.GSaveAsA;
import genedit.controller.interfaceActions.GSwitchWorkspaceA;
import genedit.controller.interfaceActions.GUndoA;
import genedit.controller.interfaceActions.GZoomInA;
import genedit.controller.interfaceActions.GZoomOutA;
import genedit.controller.interfaceActions.PropertyManager;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;

import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import bibliothek.gui.dock.common.DefaultSingleCDockable;

//TODO inicijalna podeï¿½avanja gui-a izbaciti u posebnu klasu kako bi se uï¿½itavali iz fajl-a
public class GMainMenu extends JMenuBar {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected static GMainMenu mainMenu = null;

	protected JMenu file, edit, view, help;
	protected JMenu subMenuLanguage;
	protected JRadioButton radioItemEnglish, radioItemSerbian;
	protected JMenuItem item;
	protected JCheckBoxMenuItem checkItemProjectTree, checkItemToolbar, checkItemTableView, check;
	protected JMenu subMenuWindows;
	protected JMenuItem mniProperties;
	protected JMenuItem autoLinkOptimizer;
	protected JMenuItem it;

	protected GMainMenu() {
		super();

		// //////////////////////////////////////////////////
		// kreiranje FILE menija
		file = new JMenu("File");

		it = new JMenuItem(GNewDocumentA.getInstance());
		file.add(it);

		it = new JMenuItem(GNewSubDocumentA.getInstance());
		file.add(it);

		it = new JMenuItem(GNewProjectA.getInstance());
		file.add(it);
		// file.add(GNewProjectA.getInstance());
		it = new JMenuItem(GNewPackageA.getInstance());
		file.add(it);

		GNewPackageA.getInstance().setEnabled(false);
		GNewDocumentA.getInstance().setEnabled(false);

		file.addSeparator();

		it = new JMenuItem(GOpenA.getInstance());
		file.add(it);

		file.addSeparator();

		it = new JMenuItem(GSwitchWorkspaceA.getInstance());
		file.add(it);
		file.addSeparator();

		it = new JMenuItem(GSaveA.getInstance());
		file.add(it);

		it = new JMenuItem(GSaveAsA.getInstance());
		file.add(it);

		it = new JMenuItem(GSaveAllA.getInstance());
		file.add(it);

		file.addSeparator();

		it = new JMenuItem(GPrintA.getInstance());
		file.add(it);

		file.addSeparator();

		it = new JMenuItem(GCloseA.getInstance());
		file.add(it);

		it = new JMenuItem(GCloseAllA.getInstance());
		file.add(it);

		it = new JMenuItem(GExitA.getInstance());
		file.add(it);

		add(file);

		// //////////////////////////////////////////////////
		// kreiranje EDIT menija

		edit = new JMenu("Edit");

		it = new JMenuItem(GUndoA.getInstance());
		edit.add(it);

		it = new JMenuItem(GRedoA.getInstance());
		edit.add(it);

		it = new JMenuItem(GCutA.getInstance());
		edit.add(it);

		it = new JMenuItem(GCopyA.getInstance());
		edit.add(it);

		it = new JMenuItem(GPasteA.getInstance());
		edit.add(it);

		edit.addSeparator();

		it = new JMenuItem(GDeleteA.getInstance());
		edit.add(it);
		
		edit.addSeparator();
		
		it=new JMenuItem(GGenerateCodeA.getInstance());
		edit.add(it);
		
		edit.addSeparator();
		
		autoLinkOptimizer = new JCheckBoxMenuItem("Link optimizer");
		autoLinkOptimizer.setName("LinkOptimizer");
		autoLinkOptimizer.setSelected((Boolean) PropertyManager.getInstance().getProperty(
				PropertyManager.AutoLinkOptimize));
		autoLinkOptimizer.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				PropertyManager.getInstance().setProperty(PropertyManager.AutoLinkOptimize,
						((JCheckBoxMenuItem) e.getSource()).isSelected());
				GMainFrame.getInstance().refreshAllViews();
			}
		});
		edit.add(autoLinkOptimizer);

		edit.addSeparator();

		mniProperties = new JMenuItem("Properties");
		edit.add(mniProperties);

		add(edit);

		// //////////////////////////////////////////////////
		// kreiranje VIEW menija

		view = new JMenu("View");

		it = new JMenuItem(GZoomInA.getInstance());
		view.add(it);

		it = new JMenuItem(GZoomOutA.getInstance());
		view.add(it);

		check = new JCheckBoxMenuItem("Grid");
		check.setName("Grid");
		check.setSelected((Boolean) PropertyManager.getInstance().getProperty(
				PropertyManager.GridVisible));
		check.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				PropertyManager.getInstance().setProperty(PropertyManager.GridVisible,
						((JCheckBoxMenuItem) e.getSource()).isSelected());
				GMainFrame.getInstance().refreshAllViews();
			}
		});
		view.add(check);

		subMenuWindows = new JMenu("Windows");

		// osluskivanje komponente docking frames-a za prikaz stabla
		checkItemProjectTree = new JCheckBoxMenuItem("Project Tree");
		checkItemProjectTree.setName("checkItemProjectTree");
		checkItemProjectTree.setSelected(true);
		checkItemProjectTree.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				DefaultSingleCDockable tree = (DefaultSingleCDockable) GMainFrame.getInstance()
						.getController().getSingleDockable("projectTree");
				if (((JCheckBoxMenuItem) e.getSource()).isSelected()) {
					tree.setVisible(true);
					PropertyManager.getInstance().setProperty(PropertyManager.ProjectTreeVisible,
							true);
				} else {
					tree.setVisible(false);
					PropertyManager.getInstance().setProperty(PropertyManager.ProjectTreeVisible,
							false);
				}
			}
		});

		subMenuWindows.add(checkItemProjectTree);

		// pra�enje toolbar-a
		checkItemToolbar = new JCheckBoxMenuItem("Toolbar");
		checkItemToolbar.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				if (((JCheckBoxMenuItem) e.getSource()).isSelected()) {
					GToolbar.getInstance().setVisible(true);
				} else {
					GToolbar.getInstance().setVisible(false);
				}
			}
		});
		checkItemToolbar.setSelected(true);

		subMenuWindows.add(checkItemToolbar);

		checkItemTableView = new JCheckBoxMenuItem("Table View");
		// subMenuWindows.add(checkItemTableView);

		view.add(subMenuWindows);

		subMenuLanguage = new JMenu("Language");
		subMenuLanguage.setName("subMenuLanguage");
		radioItemEnglish = new JRadioButton("English");
		radioItemSerbian = new JRadioButton("Serbian");

		ButtonGroup bg = new ButtonGroup();
		bg.add(radioItemEnglish);
		bg.add(radioItemSerbian);

		radioItemEnglish.setSelected(true);
		radioItemEnglish.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Locale.setDefault(new Locale("en", "US"));
				GMainFrame.getInstance().changeLanguage();
			}
		});
		radioItemSerbian.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Locale.setDefault(new Locale("sr", "RS"));
				GMainFrame.getInstance().changeLanguage();

			}
		});

		subMenuLanguage.add(radioItemEnglish);
		subMenuLanguage.add(radioItemSerbian);

		view.add(subMenuLanguage);

		add(view);

		// //////////////////////////////////////////////////
		// kreiranje HELP menija

		help = new JMenu("Help");
		item = new JMenuItem("About");
		help.add(item);

		add(help);
	}

	public static GMainMenu getInstance() {
		if (mainMenu == null) {
			mainMenu = new GMainMenu();
			mainMenu.initComponents();
		}
		return mainMenu;
	}

	public void initComponents() {

		file.setText(GMainFrame.getInstance().getResourceBundle().getString("File"));
		GNewDocumentA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("NewDiagram"));
		GNewSubDocumentA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("NewSubDiagram"));
		GNewProjectA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("NewProject"));
		GNewPackageA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("NewPackage"));
		GOpenA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("Open"));
		GSwitchWorkspaceA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("SwitchWorkspace"));
		GSaveA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("Save"));
		GSaveAsA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("SaveAs"));
		GSaveAllA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("SaveAll"));
		GCloseA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("Close"));
		GCloseAllA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("CloseAll"));
		GPrintA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("Print"));
		GExitA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("Exit"));

		edit.setText(GMainFrame.getInstance().getResourceBundle().getString("Edit"));
		GUndoA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("Undo"));
		GRedoA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("Redo"));
		GCutA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("Cut"));
		GCopyA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("Copy"));
		GPasteA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("Paste"));
		GDeleteA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("Delete"));
		mniProperties.setText(GMainFrame.getInstance().getResourceBundle().getString("Properties"));
		autoLinkOptimizer.setText(GMainFrame.getInstance().getResourceBundle().getString("LinkOptimizer"));
		
		view.setText(GMainFrame.getInstance().getResourceBundle().getString("View"));
		GZoomInA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("ZoomIn"));
		GZoomOutA.getInstance().putValue(AbstractAction.NAME,
				GMainFrame.getInstance().getResourceBundle().getString("ZoomOut"));
		checkItemProjectTree.setText(GMainFrame.getInstance().getResourceBundle()
				.getString("ProjectTree"));
		checkItemToolbar.setText(GMainFrame.getInstance().getResourceBundle().getString("Toolbar"));
		checkItemTableView.setText(GMainFrame.getInstance().getResourceBundle()
				.getString("TableView"));
		subMenuWindows.setText(GMainFrame.getInstance().getResourceBundle().getString("Windows"));
		subMenuLanguage.setText(GMainFrame.getInstance().getResourceBundle().getString("Language"));
		radioItemEnglish.setText(GMainFrame.getInstance().getResourceBundle().getString("English"));
		radioItemSerbian.setText(GMainFrame.getInstance().getResourceBundle().getString("Serbian"));
		findItemByName("Grid").setText(
				GMainFrame.getInstance().getResourceBundle().getString("GridVisible"));

		help.setText(GMainFrame.getInstance().getResourceBundle().getString("Help"));
		item.setText(GMainFrame.getInstance().getResourceBundle().getString("About"));
	}

	public JMenu getFile() {
		return file;
	}

	public void setFile(JMenu file) {
		this.file = file;
	}

	public JMenu getEdit() {
		return edit;
	}

	public void setEdit(JMenu edit) {
		this.edit = edit;
	}

	public JMenu getView() {
		return view;
	}

	public void setView(JMenu view) {
		this.view = view;
	}

	public JMenu getHelp() {
		return help;
	}

	public void setHelp(JMenu help) {
		this.help = help;
	}

	public JMenu getSubMenuLanguage() {
		return subMenuLanguage;
	}

	public void setSubMenuLanguage(JMenu subMenuLanguage) {
		this.subMenuLanguage = subMenuLanguage;
	}

	public JRadioButton getRadioItemEnglish() {
		return radioItemEnglish;
	}

	public void setRadioItemEnglish(JRadioButton radioItemEnglish) {
		this.radioItemEnglish = radioItemEnglish;
	}

	public JRadioButton getRadioItemSerbian() {
		return radioItemSerbian;
	}

	public void setRadioItemSerbian(JRadioButton radioItemSerbian) {
		this.radioItemSerbian = radioItemSerbian;
	}

	public JMenuItem getItem() {
		return item;
	}

	public void setItem(JMenuItem item) {
		this.item = item;
	}

	public JMenu getSubMenuWindows() {
		return subMenuWindows;
	}

	public void setSubMenuWindows(JMenu subMenuWindows) {
		this.subMenuWindows = subMenuWindows;
	}

	public JCheckBoxMenuItem getCheck() {
		return check;
	}

	public void setCheck(JCheckBoxMenuItem check) {
		this.check = check;
	}

	public JCheckBoxMenuItem getCheckItemProjectTree() {
		return checkItemProjectTree;
	}

	public void setCheckItemProjectTree(JCheckBoxMenuItem checkItemProjectTree) {
		this.checkItemProjectTree = checkItemProjectTree;
	}

	public JCheckBoxMenuItem getCheckItemToolbar() {
		return checkItemToolbar;
	}

	public void setCheckItemToolbar(JCheckBoxMenuItem checkItemToolbar) {
		this.checkItemToolbar = checkItemToolbar;
	}

	public JCheckBoxMenuItem getCheckItemTableView() {
		return checkItemTableView;
	}

	public void setCheckItemTableView(JCheckBoxMenuItem checkItemTableView) {
		this.checkItemTableView = checkItemTableView;
	}

	public JMenuItem getMniProperties() {
		return mniProperties;
	}

	public void setMniProperties(JMenuItem mniProperties) {
		this.mniProperties = mniProperties;
	}

	public JMenuItem getIt() {
		return it;
	}

	public void setIt(JMenuItem it) {
		this.it = it;
	}

	public JMenuItem findItemByName(String name) {
		for (int i = 0; i < mainMenu.getMenuCount(); i++) {
			JMenu currentMenu = mainMenu.getMenu(i);
			JMenuItem result = findItemByNameInSubmenu(name, currentMenu);
			if (result != null)
				return result;
		}
		return null;
	}

	protected JMenuItem findItemByNameInSubmenu(String name, JMenu subMenu) {
		for (int i = 0; i < subMenu.getItemCount(); i++) {
			JMenuItem item = subMenu.getItem(i);
			if (item != null) {
				if (item.getName() != null) {
					if (item.getName().equals(name)) {
						return item;
					}
				}
				if (item instanceof JMenu) {
					JMenuItem result = findItemByNameInSubmenu(name, (JMenu) item);
					if (result != null)
						return result;
				}
			}
		}
		return null;
	}

}